//
// Created by jingyuewu on 25-8-17.
//
#include <iostream>
#include <functional>
#include <boost/asio.hpp>

#include "debug_utils.h"

class Printer {
public:
    Printer(boost::asio::io_context& io)
        : timer_(io, boost::asio::chrono::seconds(1)),
        count_(0)
    {
        timer_.async_wait(std::bind(&Printer::print, this));
    }

    ~Printer() {
        DEMO_DEBUG("Final count_ is " + std::to_string(count_));
    }

    void print() {
        DEMO_DEBUG("printer's member function");
        if (count_ < 5) {
            DEMO_DEBUG(std::to_string(count_));
            ++count_;

            timer_.expires_at(timer_.expiry() + boost::asio::chrono::seconds(1));
            timer_.async_wait(std::bind(&Printer::print, this));
        }
    }

private:
    boost::asio::steady_timer timer_;
    int count_;
};

int main(int argc, char **argv) {
    boost::asio::io_context io;
    Printer printer(io);
    io.run();
    return 0;
}
